package com.aiguigu.interactive;

// V1 synchronized + wait + notifyAll 传统老版本
//

import ch.qos.logback.classic.spi.EventArgUtil;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class AirCondition //资源类（空调） OOP = Field + Method
{    //V1 synchronized + wait + notifyAll 传统老版本
    /*private int number = 0;// 有一个初始值为零的变量

    public  synchronized void increment() throws InterruptedException
    {
        while(number!=0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+" "+(++number));
        this.notifyAll();


    }

    public synchronized void decrement() throws InterruptedException
    {
        while(number==0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+" "+(--number));
        this.notifyAll();
    }*/


    ////V2 JUC新版，Lock + condition.await() + condition.signalAll()
    private int number = 0;
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void increment() throws InterruptedException {
        lock.lock();
        try {
          while (number!=0){
              condition.await();
          }
          number++;
            System.out.println(Thread.currentThread().getName()+"  "+number);
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }

    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            while (number==0){
                condition.await();
            }
            number--;
            System.out.println(Thread.currentThread().getName()+"  "+number);
            condition.signalAll();
        }finally{
            lock.unlock();
        }
    }
}

/**
 * @auther zzyybs@126.com
 * @Date 2025-04-22 8:19
 * @Description: 有一个初始值为零的变量，现有两个线程对该变量操作，
 * 实现一个线程对变量加1，实现一个线程对变量减1,交替来10轮
 */
public class ProdConsumerDemo {
    public static void main(String[] args) {
        AirCondition airCondition = new AirCondition();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    airCondition.increment();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    airCondition.decrement();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    airCondition.increment();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "C").start();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    airCondition.decrement();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "D").start();
    }
}